import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;

public class LogicClient extends Thread implements Client {

	// variables
	private Socket socket;
	private ArrayList<String> gameMoves = null;

	// reader
	private BufferedReader bufferedReader = null;

	public LogicClient() {
		this.gameMoves = new ArrayList<String>();
	}

	public boolean connect(String ip, int port) {

		try {
			socket = new Socket(ip, port);
			System.out.println("3.1");
			// initialize reader
			bufferedReader = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			System.out.println("3.2");
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public String read() {
		String message;
		String f = "failed";
		try {
			message = bufferedReader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			return f;
		}
		gameMoves.add(message);
		System.out.println(message);
		return message;
	}

	public void run() {
		while (true) {
			 System.out.println("reading...");
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			read();
		}
	}

	public String getNextMove() {
		if (gameMoves.size() == 0) {
			return null;
		} else {
			return gameMoves.remove(0);
		}
	}

	public int getListSize() {
		return gameMoves.size();
	}
}
